package com.chwn.curiousdiary.ui.play.fragment;

import android.app.Activity;
import android.content.DialogInterface;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.Toast;

import com.chwn.curiousdiary.R;
import com.chwn.curiousdiary.gloable.CustomEvent;
import com.chwn.harulib.ScrollerNumberPicker;
import com.umeng.analytics.MobclickAgent;

import java.util.Arrays;
import java.util.HashMap;

import de.greenrobot.event.EventBus;

/**
 * Created by Myshikin on 2017/7/20.
 */

public class TimingFragment extends DialogFragment {

    public Activity mContext;

    private ImageView mBgIV;

    private ScrollerNumberPicker mTimming;

//    private TextView mMaskTV;

    private int bgType;

    private int[] bgIds = new int[]{R.drawable.bg_queue_adventure, R.drawable.bg_queue_build, R.drawable.bg_queue_fancy
            , R.drawable.bg_queue_life, R.drawable.bg_queue_china, R.drawable.bg_queue_animal, R.drawable.bg_queue_adventure,};

    private String[] timmingTimes = new String[]{"无", "15", "30", "45", "60", "90"};
    private int[]    timmingTime  = new int[]{0, 15 * 60 * 1000, 30 * 60 * 1000, 45 * 60 * 1000, 60 * 60 * 1000, 90 * 60 * 1000};
    private int position;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        this.mContext = activity;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(DialogFragment.STYLE_NO_FRAME, R.style.CustomDatePickerDialog);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        //设置无标题
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        //设置从底部弹出
        Window window = getDialog().getWindow();
        WindowManager.LayoutParams params = window.getAttributes();
        params.gravity = Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL;
        window.requestFeature(Window.FEATURE_NO_TITLE);
        window.setAttributes(params);
        window.setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.play_queue_bg)));

        View view = inflater.inflate(R.layout.fragment_timing, container);
        mBgIV = (ImageView) view.findViewById(R.id.iv_bg);
        mTimming = (ScrollerNumberPicker) view.findViewById(R.id.timming);
//        mMaskTV = (TextView) view.findViewById(R.id.min_mask);
        view.findViewById(R.id.iv_close_queue).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
            }
        });

        mTimming.setOnSelectListener(new ScrollerNumberPicker.OnSelectListener() {
            @Override
            public void endSelect(int id, String text) {
               /* if ("无".equals(text)) {
                    *//*mMaskTV.post(new Runnable() {
                        @Override
                        public void run() {
                            if (mMaskTV.getVisibility() == View.VISIBLE) {
                                mMaskTV.setVisibility(View.GONE);
                            }
                        }
                    });*//*
                } else if (mMaskTV.getVisibility() == View.GONE) {
                    *//*mMaskTV.post(new Runnable() {
                        @Override
                        public void run() {
                            if (mMaskTV.getVisibility() == View.GONE) {
                                mMaskTV.setVisibility(View.VISIBLE);
                            }
                        }
                    });*//*
                }*/
            }

            @Override
            public void selecting(int id, String text) {
            }
        });
        mTimming.setAddMark(true);

        mTimming.setData(Arrays.asList(timmingTimes));
        return view;
    }

    @Override
    public void onStart() {
        super.onStart();
        //设置fragment高度 、宽度
        int dialogHeight = (int) (mContext.getResources().getDisplayMetrics().heightPixels * 0.4);
        getDialog().getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT, dialogHeight);
        getDialog().setCanceledOnTouchOutside(true);
        getData();
    }

    private void getData() {
        Bundle arguments = getArguments();
        bgType = arguments.getInt("bgType", 0);
        mBgIV.setBackgroundResource(bgIds[bgType]);
        position = arguments.getInt("position" , 0);
        mTimming.setDefault(position);
//        mMaskTV.setVisibility(position == 0 ? View.GONE : View.VISIBLE);
    }


    @Override
    public void onDismiss(DialogInterface dialog) {
        super.onDismiss(dialog);
        int selected = mTimming.getSelected();
        if (position != selected && selected != 0) {
            EventBus.getDefault().post(new CustomEvent(CustomEvent.TIMMING_START, selected));
            Toast.makeText(mContext, "将在" + timmingTimes[selected] + "分钟后停止播放", Toast.LENGTH_SHORT).show();
            HashMap<String, String> map = new HashMap<>();
            map.put("type", timmingTimes[selected]);
            MobclickAgent.onEvent(getContext(),"timming_config_count", map);
        } else if (position != 0 && selected == 0) {
            EventBus.getDefault().post(new CustomEvent(CustomEvent.TIMMING_STOP));
            Toast.makeText(mContext, "关闭定时服务", Toast.LENGTH_SHORT).show();
        }
    }
}
